package stream;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

class Students {
    private String name;
    private int age;
    private int score;

    public Students() {
    }

    public Students(String name, int age, int score) {
        this.name = name;
        this.age = age;
        this.score = score;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }

    @Override
    public String toString() {
        return "Students{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", score=" + score +
                '}';
    }
}

class StudentsComparator implements Comparator<Students> {
    @Override
    public int compare(Students s1, Students s2) {
        if(s1.getAge()==s2.getAge()){
            return s2.getScore()-s1.getScore();
        }else{
            return s1.getAge() - s2.getAge();
        }
    }
}
public class StreamDemo {

    public static void main(String[] args) {
        String s = "I love you too too";
        String[] arr = s.split(" ");
        List<String> list = Arrays.asList(arr);
        System.out.println("--------输出每一个元素---------");
        list.stream().forEach(System.out::println);
        System.out.println("--------输出长度为3的字符串---------");
        list.stream().filter(str -> str.length() == 3).forEach(System.out::println);
        System.out.println("--------去除重复的字符串---------");
        list.stream().distinct().forEach(System.out::println);
        System.out.println("------每一个元素转换为大写字母------");
        list.stream().map(str -> str.toUpperCase()).forEach(System.out::println);
        System.out.println("-----------输出元素个数-----------");
        System.out.println(list.stream().count());

        List<Students> list2 = new ArrayList<Students>();
        list2.add(new Students("张三", 18, 89));
        list2.add(new Students("孙琦", 16, 80));
        list2.add(new Students("李四", 18, 99));
        list2.add(new Students("王五", 17, 79));
        System.out.println("-----------按照学生年龄排序-----------");
        list2.stream().sorted(Comparator.comparing(Students::getAge)).forEach(System.out::println);

        System.out.println("-----------先按照学生年龄排序再按照考试分数降序排序-----------");
        list2.stream().
                sorted(Comparator.comparing(Students::getAge).thenComparing(Students::getScore))
                .forEach(System.out::println);

        System.out.println("-----------先按照学生年龄排序再按照考试分数降序排序(自定义排序规则)-----------");
        list2.stream().sorted(new StudentsComparator()).forEach(System.out::println);
    }
}
